<template>
<div class="vip-goods-list views-container">
    <el-dialog @close="saveDialogForm" :visible.sync="dialogTableVisible" custom-class="reset-pwd wlm-form-dialog dialog-table">
        <div class="wlm-form" v-if="dialogTableVisible">
            <div class="wlm-form-header wlm-dialog-fixheader">
                <span class="wlm-dialog-inlenr">设置会员价</span>
            </div>
            <el-scrollbar wrap-class="scrollbar-wrapper">
                <div class="wlm-form-content">
                    <div class="wlm-dialog-main">
                        <div class="wlm-dialog-content">
                            <el-form ref="user" :model="formFormatData.formData" :rules="formFormatData.rules" size="small" label-width="120px" class="retail-form" label-position="right">
                                <el-form-item label="会员价状态">
                                    <el-radio-group v-model="formFormatData.formData.is_member_price" @change='typeChange'>
                                        <el-radio :label="1">启用</el-radio>
                                        <el-radio :label="2">不启用</el-radio>
                                    </el-radio-group>
                                </el-form-item>
                                <el-form-item label="会员商品" v-if="formFormatData.formData.is_member_price==1">
                                    <el-table :data="formFormatData.formData.goods" style="width: 100%">
                                        <el-table-column label="商品规格">
                                            <template slot-scope="scope">
                                                <span>{{scope.row.title || '-'}}</span>
                                            </template>
                                        </el-table-column>
                                        <el-table-column label="价格">
                                            <template slot-scope="scope">
                                                <span class="orders-red">￥{{scope.row.goods_price}}</span>
                                            </template>
                                        </el-table-column>
                                        <el-table-column label="设置会员价">
                                            <template slot-scope="scope">
                                                <!-- <div class="input-slot flex-row flex-align-c flex-justify-s gc-append-box vipbox">
                                                    <el-input-number style="width:120px;" @blur="inputBlur(scope.row.member_price)" :controls="false" size="small" :min="0" :max="parseFloat(scope.row.goods_price-0.01)" v-model="scope.row.member_price"></el-input-number>
                                                    <div class="gc-append gc-append-right">元</div>
                                                </div> -->
                                                <div class="input-slot flex-row flex-align-c flex-justify-s gc-append-box">
                                                    <el-input-number style="width:100px;" @input="inputBlur(scope.row.member_price)" :controls="false" size="small" :min="0" :max="parseFloat(scope.row.goods_price-0.01)" v-model="scope.row.member_price"></el-input-number>
                                                    <div class="gc-append gc-append-right">元</div>
                                                </div>
                                            </template>
                                        </el-table-column>
                                    </el-table>
                                </el-form-item>
                            </el-form>
                        </div>
                    </div>
                </div>
            </el-scrollbar>
        </div>
        <footer class="flex-row flex-align-c flex-justify-c wlm-dialog-fixfooter">
            <el-button size="small" @click="ruleCheck" type="primary">保存</el-button>
        </footer>
    </el-dialog>
    <div class="wlm-table">
        <div class="wlm-table-header">
            <el-form :model="tableFormatData.announcement.files" size="small" label-width="95px" class="retail-form" label-position="right">
                <el-form-item label="" class="filter">
                    <el-form-item label="商品名称：" class="search">
                        <el-input @keyup.enter.native="filesSerch" v-model="tableFormatData.announcement.files.name" placeholder="请输入商品名称" style="width:200px;"></el-input>
                    </el-form-item>
                    <el-form-item label="商品分类：" class="filter">
                        <el-cascader :props="myProps" :options="categoryData" :show-all-levels="false" v-model="category_array" @change="categoryChange">
                        </el-cascader>
                    </el-form-item>
                </el-form-item>
                <el-form-item label="" class="filter-btns">
                    <el-button type="primary" @click="filesSerch">筛选</el-button>
                    <el-button type="text" @click="filesEmptyBefore">清空筛选条件</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="wlm-table-content">
            <el-table :ref="tableFormatData.announcement.key" :data="tableFormatData.announcement.tableData" @selection-change="handleSelectionChange" style="width: 100%">
                <!-- <el-table-column type="selection" width="55">
                </el-table-column> -->
                <el-table-column prop="sort" width="80" label="排序" sortable>
                    <template slot-scope="scope">
                        <el-input-number @blur="changeTableItem({ids:[scope.row.goods_id],goods_sort:scope.row.goods_sort},tableFormatData.announcement.change.sort)" style="width:100%;" size="mini" v-model="scope.row.goods_sort" :controls="false"></el-input-number>
                    </template>
                </el-table-column>
                <el-table-column prop="date" label="商品LOGO" min-width="30">
                    <template slot-scope="scope" v-if="Array.isArray(scope.row.image) &&scope.row.image.length>0">
                        <div class="wlm-table-logos">
                            <img class="logo" :src="scope.row.image[0]['file_path']">
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="date" label="商品名称">
                    <template slot-scope="scope">
                        <span>{{scope.row.goods_name}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="商品分类">
                    <template slot-scope="scope" v-if="scope.row.category_data">
                        <p style="white-space: nowrap;" v-for="(item, index) in scope.row.category_data" :key="index" v-if="item.name&&scope.row.category_data">{{`${item.name}: ${item.child?item.child.length>1?item.child.join('/'):item.child[0]:''}`}}</p>
                        <p v-if="scope.row.category_data.length==0">-</p>
                    </template>
                </el-table-column>
                <el-table-column label="商品状态" min-width="50">
                    <template slot-scope="scope">
                        <span v-if="scope.row.goods_status=='10'">售卖中</span>
                        <span v-if="scope.row.goods_status=='20'">已下架</span>
                        <span v-if="scope.row.goods_status=='30'">已售罄</span>
                    </template>
                </el-table-column>
                <el-table-column label="会员专属" min-width="50">
                    <template slot-scope="scope">
                        <el-switch :active-value="1" :inactive-value="2" @change="changeTableItem({goods_id:scope.row.goods_id},tableFormatData.announcement.change.member_exclusive,{isWarning: scope.row.member_exclusive === 2,warning:'是否关闭会员专属？',cancel:{msg:'已取消关闭',val:{scope:scope,cancelStatus:{key:'member_exclusive',val:1, error: 2}}}})" v-model="scope.row.member_exclusive"> </el-switch>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="会员价" min-width="50">
                    <template slot-scope="scope">
                        <p v-if="scope.row.member_price"><span class="orders-red">￥</span><span>{{scope.row.member_price}}</span></p>
                        <p class="orders-gray" v-else>0</p>
                    </template>
                </el-table-column>
                <el-table-column label="会员价状态" min-width="50">
                    <template slot-scope="scope">
                     <!-- changeTableItem({goods_id:scope.row.goods_id},
      //                   tableFormatData.announcement.change.is_member_price,
      //                   {isWarning: scope.row.is_member_price === 2,warning:'确定关闭吗？',cancel:{msg:'已取消关闭',val:{scope:scope,cancelStatus:{key:'is_member_price',val:1, error: 2}}}}) -->
                        <el-switch :active-value="1" :inactive-value="2" :disabled="scope.row.is_member_price==2" @change="changeTableItem({goods_id:scope.row.goods_id},tableFormatData.announcement.change.is_member_price,{isWarning: scope.row.is_member_price === 2,warning:'确定关闭吗？',cancel:{msg:'已取消关闭',val:{scope:scope,cancelStatus:{key:'is_member_price',val:1, error: 2}}}})" v-model="scope.row.is_member_price"> </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <div class="operation-group">
                            <el-button class="wlm-text" type="text" @click="setVipPrice(scope.row.is_member_price,scope.row.sku,{id:scope.row.goods_id,toggle:'dialogTableVisible',noEdit: true})">设置会员价</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-content flex-row flex-justify-e flex-align-c">
                <!-- <el-row class="pagination-btns">
                    <el-checkbox class="check-all" true-label="1" false-label="0" v-model="tableFormatData.announcement.files.checkall" @change="toggleSelection">全部</el-checkbox>
                    <el-button class="right-8" :disabled="isGroup" size="mini" @click="delTableList">删除</el-button>
                </el-row> -->
                <el-pagination :disabled="!hasTableData" @size-change="listPageChange" @current-change="listPageChange" :current-page.sync="tableFormatData.announcement.pagination.page" :page-sizes="tableFormatData.announcement.pagination.pagesizes" :page-size.sync="tableFormatData.announcement.pagination.list_rows" layout="total, sizes, prev, pager, next, jumper" :total="hasTableData?tableFormatData.announcement.pagination.total : 0">
                </el-pagination>
            </div>
        </div>
    </div>
</div>
</template>

<script>
import {
  MemberGoodsStatus,
  setMemberGoodsPrice,
  exclusive
} from '@/api/application'
import {
  goodsIndex,
  categoryIndex,
  goodsSort
} from '@/api/merchandise'
import mixins from '@/mixins/mixins'
export default {
  mixins: [mixins.getters('Table'), mixins.getters('Form')],
  name: 'VipGoodsList',
  components: {
  },
  created() {
    this.$nextTick(() => {
      categoryIndex().then((response) => {
        const {
          data: {
            data: msgData = []
          }
        } = response
        this.categoryData = msgData
      })
      this.inputBlur()
    })
  },
  data() {
    return {
      category_array: [],
      categoryData: [],
      nowSave: false,
      myProps: {
        value: 'category_id',
        label: 'name',
        children: 'child'
      },
      dialogTableVisible: false,
      selecStoreData: [],
      blogTitle: '',
      formFormatData: {
        key: 'userForm',
        model: {
          noCreateEdit: false,
          bindDialog: 'dialogTableVisible'
        },
        api: {
          editForm: {
            params: {
              id: ''
            },
            redirect: 'goods_id'
          },
          submitForm: setMemberGoodsPrice
        },
        formData: {
          ids: [],
          is_member_price: 1,
          goods: []
        },
        rules: {
          oldPassword: [{
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }]
        }
      },
      tableFormatData: {
        current: 'announcement',
        announcement: {
          key: 'announcement',
          model: {
            bindDialog: 'dialogTableVisible'
          },
          api: {
            getList: goodsIndex
          },
          tableData: [],
          files: {
            name: '',
            category_id: '',
            indexType: 'card',
            sale_type: 0,
            ids: [],
            checkall: '0',
            Recycle: '1'
          },
          change: {
            is_member_price: {
              key: 'is_member_price',
              api: MemberGoodsStatus
            },
            member_exclusive: {
              key: 'member_exclusive',
              api: exclusive
            },
            sort: {
              name: '排序',
              key: 'sort',
              api: goodsSort
            }
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      }
    }
  },
  methods: {
      // changeTableItem({goods_id:scope.row.goods_id},
      //                   tableFormatData.announcement.change.is_member_price,
      //                   {isWarning: scope.row.is_member_price === 2,warning:'确定关闭吗？',cancel:{msg:'已取消关闭',val:{scope:scope,cancelStatus:{key:'is_member_price',val:1, error: 2}}}})
    changebtn(goods_id,is_member_price){
      console.log("dddddddddddddddddddddddddddddddddd",is_member_price ,goods_id)
      // if(is_member_price==1){
      //   MemberGoodsStatus({goods_id:goods_id}).then(res=>{
      //     if(res.data.code==1){
      //       this.$message.success(res.data.msg)
      //     }else{
      //       this.$message.error(res.data.msg)
      //     }
      //   })
      // }else{
      //    this.$confirm('确定关闭吗？', '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(() => {

      //   }).catch(() => {
      //       console.log("6666666666666666666666666666",is_member_price)
      //       is_member_price==2?is_member_price=1:is_member_price=2

            
      //   });
      // }
    },
    ruleCheck() {
      if (this.nowSave) {
        this.formFormatSubmit()
      } else {
        this.$message({
          message: '请设置会员价',
          type: 'warning'
        })
      }
    },
    typeChange() {
      if (this.formFormatData.formData.is_member_price === 1) {
         this.formFormatData.formData.goods.forEach(item => {
          console.log("dssssssssssaaaaaaaaaaaaaaaaffffffffffffff",item)
          item.member_price = item.goods_price-0.01
          
      });
        this.nowSave = true

      } else {
        this.nowSave = true
      }
    },
    inputBlur(setprice) {
      // 如果启用会员价状态，并且设置会员价=0并且=''时
      console.log("setprice+++++++++++++++++++",setprice)
      if (this.formFormatData.formData.is_member_price == 1 && (setprice != 0 && setprice == '')) {
        this.nowSave = false
      } else {
        this.nowSave = true
      }
      console.log('sssssssssssssssssssssssssss',this.nowSave)
    },
    saveDialogForm() {
      this.resetDialogFormData({ goods: [], is_member_price: 1 })
    },
    filesEmptyBefore() {
      this.category_array = []
      this.filesEmpty()
    },
    categoryChange(category_array) {
      const [category_id] = category_array.slice(-1)
      this.tableFormatData.announcement.files.category_id = category_id
    },
    setVipPrice(isMemberPrice, data, params) {
      this.formFormatData.formData.is_member_price = isMemberPrice
      data.map(item => {
        this.formFormatData.formData.goods.push({ title: item.title, goods_sku_id: item.goods_sku_id, goods_price: item.goods_price, member_price: item.member_price })
      })
      this.formFormatDialogEditEvt(params)
    }
  }
}
</script>

<style lang="scss" scoped>
.vip-goods-list {
    /deep/ .gc-append-box .el-input__inner {
        border-bottom-right-radius: 0;
        border-top-right-radius: 0;
    }

    /deep/ .gc-append-box .el-input-number {
        width: 100px;
    }

    /deep/ .gc-append-box .el-input-number .el-input {
        width: 100px;
    }

    .gc-append {
        height: 32px;
        padding: 0 13px;
        text-align: center;
        line-height: 32px;
        background: #f5f7fa;
        border: 1px solid #dcdfe6;
        box-sizing: border-box;
        border-right-color: transparent;
        border-left-color: transparent;
        color: #909399;
    }

    .gc-append-right {
        border-right-color: #dcdfe6;
        border-bottom-right-radius: 4px;
        border-top-right-radius: 4px;
    }

    .gc-append-left {
        border-left-color: #dcdfe6;
        border-bottom-left-radius: 4px;
        border-top-left-radius: 4px;
    }
}
</style>
